<?php

/*

   Aiesec's OH Management System - to control the Office Hour of Aiesec's Members.
   Copyright (C) 2009 Vinicius Stein Dani

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation; either version 2
   of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

*/

/*
  
   Author: Vinicius Stein Dani
   Contact: viniciussdani@gmail.com
   Today is: March 15, 2009
   Location at this present moment: Santa Maria - RS - Brasil
  
*/

?>

<?php

if($a == "membros")
{
   $nome_dir = "membros";
   
   $novo_x = 100;
   $novo_y = 100;
   
   $novo_exibicao_x = 500;
   $novo_exibicao_y = 500;
}
else if($a == "times")
{
   $nome_dir = "times";
   
   $novo_x = 148;
   $novo_y = 148;
   
   $novo_exibicao_x = 500;
   $novo_exibicao_y = 500;
}
else if($a == "imagens")
{
   $nome_dir = "galerias";
   
   $novo_x = 148;
   $novo_y = 148;
   
   $novo_exibicao_x = 500;
   $novo_exibicao_y = 500;
}
else if($a == "parceiros")
{
   $nome_dir = "parceiros";
   
   $novo_x = 20;
   $novo_y = 20;
   
   $novo_exibicao_x = 500;
   $novo_exibicao_y = 500;
}
else if($a == "")
{
   $nome_dir = "noticias";
   
   $novo_x = 100;
   $novo_y = 100;
   
   $novo_exibicao_x = 500;
   $novo_exibicao_y = 500;
}
else
   return;

$dir = opendir( $nome_dir );
$pics = array();
while( $fname = readdir( $dir ) )
{    
  if ((preg_match( "/[.]jpg$/", $fname))&&(strpos($fname, "@")===false ))
    $pics []= $fname;
  else if ((preg_match( "/[.]png$/", $fname))&&(strpos($fname, "@")===false ))
    $pics []= $fname;

}
closedir( $dir );

foreach( $pics as $fname )
{
  $im = imagecreatefromjpeg( "$nome_dir/$fname" );
  $im2 = imagecreatefromjpeg( "$nome_dir/$fname" );
  
  $ox = imagesx( $im );
  $oy = imagesy( $im );
  
  $ox2 = imagesx( $im2 );
  $oy2 = imagesy( $im2 );

  if($a == "parceiros")
  {
    $ny = $novo_y;
    $nx = floor( $ox * ( $novo_x / $oy ) );  
  }
  else
  {
    $nx = $novo_x;
    $ny = floor( $oy * ( $novo_y / $ox ) );
  }
 
  if($ox2 > 500)
  {
     $nx2 = $novo_exibicao_x;
     $ny2 = floor( $oy2 * ( $novo_exibicao_y / $ox2 ) );
  }
  else
  {
     $nx2 = $ox2;
     $ny2 = $oy2;
  }

  $nm = imagecreatetruecolor( $nx, $ny );
  $nm2 = imagecreatetruecolor( $nx2, $ny2 );
  
  imagecopyresampled( $nm, $im, 0, 0, 0, 0, $nx, $ny, $ox, $oy );
  imagecopyresampled( $nm2, $im2, 0, 0, 0, 0, $nx2, $ny2, $ox2, $oy2 );
  
  imagejpeg( $nm, "$nome_dir/thumbnail/@$fname", 100 );
  imagejpeg( $nm2, "$nome_dir/exibicao/@$fname", 100 );

  unlink("$nome_dir/$fname");
}

?>